let errinfo = require('./../errinfo.js');
let responseJsonUtils = require('../utils/responseJsonUtils.js');

module.exports = function (req, res, next) {
    // console.log('session id', req.sessionID);
    // console.log('req url', req.url);
    let sessionId = req.headers['book-session-id'];
    // if (req.session.user) { // check whether user has logined.
    if (sessionId) { // check whether user has logined.
        // session user exist
        // check whether session id is out of date
        sessionStore.get(sessionId, function (err, sessionResult) {
            if (err) {
                next(responseJsonUtils.json(errinfo.mongodb.internalError));
                return;
            }
            if (sessionResult) {
                // find login- session
                req.session.user = sessionResult.user;
                next();
            } else {
                //
                // console.log('sessionResult not exist');
                intercept(req, res, next);
            }
        });
    } else {
        // not login
        intercept(req, res, next);
    }
};

// intercept function
function intercept(req, res, next) {
    // 解析用户请求的路径
    let arr = req.url.split('/');
    // 去除 GET 请求路径上携带的参数
    for (let i = 0, length = arr.length; i < length; i++) {
        arr[i] = arr[i].split('?')[0];
    }
    if (arr.length < 2) {
        // url error
        next(responseJsonUtils.json(errinfo.urlError, 404));
    } else if (arr.length >= 2) {
        if (arr[1] === 'admin') {
            // admin web
            next();
            // return;
        } else if (arr[1] === 'bookserver') {
            if (arr.length === 2) {
                if (arr[1] === 'bookserver') {
                    // url is /bookserver
                    next(responseJsonUtils.json(errinfo.userLoginSessionTimeout, 401));
                } else {
                    // url error
                    next(responseJsonUtils.json(errinfo.urlError, 404));
                }
            } else if (arr.length === 3) {
                // to login
                next(responseJsonUtils.json(errinfo.userLoginSessionTimeout, 401));
            } else if (arr.length >= 4) {
                if (arr[1] == 'bookserver') {
                    // admin access url
                    if (arr[2] === 'user') {
                        if (arr[3] === 'login' || arr[3] === 'logout') {
                            next();
                        } else {
                            next(responseJsonUtils.json(errinfo.userLoginSessionTimeout, 401));
                        }
                    } else if (arr[2] === 'book') {
                        if (req.method.toUpperCase() === 'GET') {
                            next();
                        }
                    } else {
                        next(responseJsonUtils.json(errinfo.userLoginSessionTimeout, 401));
                    }
                } else {
                    // error url
                    next(responseJsonUtils.json(errinfo.urlError, 404));
                    // return;
                }
            }
        }
    }

}
